package com.travelsmart.client.apresentacao.view;








import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;

import com.travelsmart.client.apresentacao.controller.CommandController;

import com.travelsmart.client.apresentacao.i18n.I18nMessages;
import com.travelsmart.client.apresentacao.view.subview.ViewInfoPesquisaHotel;
import com.travelsmart.client.apresentacao.view.subview.ViewInfoQualidadePesquisaHotel;
import com.travelsmart.client.apresentacao.view.subview.ViewInfoQuantidadePesquisaHotel;
import com.travelsmart.client.apresentacao.widget.FormButton;
import com.travelsmart.client.apresentacao.widget.FormWidget;
import com.travelsmart.client.bean.to.FilterPesquisaHotel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;

public class ViewPesquisaHotel extends BaseView {

	private final VerticalPanel panelPesquisarHotel = new VerticalPanel();
	private CommandController<FilterPesquisaHotel> handler = null;
	private CommandController<FilterPesquisaHotel> handler2 = null;
	private CommandController<FilterPesquisaHotel> handler3 = null;
	private final ViewInfoPesquisaHotel viewInfoPesquisaHotel = new ViewInfoPesquisaHotel();
	private final ViewInfoQualidadePesquisaHotel viewInfoQualidadePesquisaHotel = new ViewInfoQualidadePesquisaHotel();
	private final ViewInfoQuantidadePesquisaHotel viewInfoQuantidadePesquisaHotel = new ViewInfoQuantidadePesquisaHotel();
	private final FormButton btn_pesquisar = new FormButton("<span id='form_button_pesquisa'>"+I18nMessages.INSTANCE.pesquisa()+"</span>", null,
			new FormWidget.HandlerValidation<String>() {
				public boolean handle(String valor) {
					boolean retorno = true;
					if (viewInfoPesquisaHotel.validar()) {
						// INICIO - EXERC. 3
						FilterPesquisaHotel filter = new FilterPesquisaHotel();
						viewInfoPesquisaHotel.carregarFilterPesquisaHotel(filter);
						viewInfoQualidadePesquisaHotel.carregarFilterPesquisaHotel(filter);
						viewInfoQuantidadePesquisaHotel.carregarFilterPesquisaHotel(filter);
						handler.handleSuccess(filter);
						// FIM - EXERC. 3
					} else {
						retorno = false;
					}
					return retorno;
				}
			});
	 


	public ViewPesquisaHotel(final CommandController<FilterPesquisaHotel> handler1, final CommandController<FilterPesquisaHotel> handler2, final CommandController<FilterPesquisaHotel> handler3) {
		super(new HTML(I18nMessages.INSTANCE.pesquisa_hotel()));
		this.handler = handler1;
		this.handler2 = handler2;
		this.handler3 = handler3;
		
		initWidget(panelPesquisarHotel);
		panelPesquisarHotel.setWidth("100%");
	}
	
	public ViewPesquisaHotel(final CommandController<FilterPesquisaHotel> handler1) {
		super(new HTML(I18nMessages.INSTANCE.pesquisa_hotel()));
		this.handler = handler1;
		
		initWidget(panelPesquisarHotel);
		panelPesquisarHotel.setWidth("100%");
	}

	


	public void exec() {
		Grid main = new Grid(2, 4);
		// Create a table to layout the form options
		viewInfoPesquisaHotel.exec();
		main.setWidget(0, 0, viewInfoPesquisaHotel);
//		main.setWidget(0, 1, infoAvancada());
		viewInfoQuantidadePesquisaHotel.exec();
		viewInfoQualidadePesquisaHotel.exec();
		main.setWidget(0, 1, viewInfoQualidadePesquisaHotel);
		main.setWidget(1, 1, viewInfoQuantidadePesquisaHotel);
		main.setWidget(1, 3, btn_pesquisar);
		formatMain(main);
		panelPesquisarHotel.add(main);
	}

	private Button openButtonDialogBoxViewInfoQuantidadePesquisaHotel() {
		viewInfoQuantidadePesquisaHotel.exec();
		final DialogBox dialogBoxViewInfoQuantidadePesquisaHotel = createDialogBox(viewInfoQuantidadePesquisaHotel, handler2);
		Button openButtonDialogBoxViewInfoQuantidadePesquisaHotel = new Button(viewInfoQuantidadePesquisaHotel.getNome().getText(), new ClickHandler() {
		          public void onClick(ClickEvent sender) {
		        	  dialogBoxViewInfoQuantidadePesquisaHotel.center();
		        	  dialogBoxViewInfoQuantidadePesquisaHotel.show();
		          }
		        });
		return openButtonDialogBoxViewInfoQuantidadePesquisaHotel;
	}

	private Button openButtonDialogBoxViewInfoQualidadePesquisaHotel() {
		viewInfoQualidadePesquisaHotel.exec();
		final DialogBox dialogBoxViewInfoQualidadePesquisaHotel = createDialogBox(viewInfoQualidadePesquisaHotel, handler3);
		Button openButtonDialogBoxViewInfoQualidadePesquisaHotel = new Button(viewInfoQualidadePesquisaHotel.getNome().getText(), new ClickHandler() {
		          public void onClick(ClickEvent sender) {
		        	  dialogBoxViewInfoQualidadePesquisaHotel.center();
		        	  dialogBoxViewInfoQualidadePesquisaHotel.show();
		          }
		        });
		return openButtonDialogBoxViewInfoQualidadePesquisaHotel;
	}
	
	public HTML getNome() {
		return this.nome;
	}
	
	
	private void formatMain(Grid main){
		main.setWidth("100%");
		main.getCellFormatter().setVerticalAlignment(0, 2,
				HasVerticalAlignment.ALIGN_TOP);
		main.getCellFormatter().setHorizontalAlignment(1, 3,
				HasHorizontalAlignment.ALIGN_RIGHT);
		main.getCellFormatter().setVerticalAlignment(0, 1,
				HasVerticalAlignment.ALIGN_TOP);
		main.getCellFormatter().setHorizontalAlignment(0, 1,
				HasHorizontalAlignment.ALIGN_LEFT);
	}
	
	private DisclosurePanel infoAvancada(){
		DisclosurePanel infoAvancada = new DisclosurePanel("Pesquisa avançada");
		infoAvancada
				.setStyleName("gwt-DisclosurePanel travelsmart-DisclosurePanel");
		// INICIO - EXERC. 3
		infoAvancada.setAnimationEnabled(true);
		Grid grid = new Grid(2, 1);
		grid.setCellSpacing(6);
		infoAvancada.setContent(grid);
		grid.setWidget(0, 0, openButtonDialogBoxViewInfoQualidadePesquisaHotel());
		grid.setWidget(1, 0, openButtonDialogBoxViewInfoQuantidadePesquisaHotel());
		// FIM - EXERC. 3
		return infoAvancada;
	}
	
	private DialogBox createDialogBox(BaseView view, final CommandController<FilterPesquisaHotel> handlerAva) {
		
	    // Create a dialog box and set the caption text
	    final DialogBox dialogBox = new DialogBox();
	 // INICIO - EXERC. 3
	    dialogBox.ensureDebugId("cwDialogBox");
	    
	    // Create table to layout the content
	    VerticalPanel dialogContents = new VerticalPanel();
	    dialogContents.setSpacing(4);
	    dialogBox.setWidget(dialogContents);
	    
	    // Add some text to the top of the dialog
	    HTML details = view.getNome();
	    dialogContents.add(details);
	    dialogContents.setCellHorizontalAlignment(
	    		details, HasHorizontalAlignment.ALIGN_CENTER);
	    
	    // Add an view to the dialog
	    dialogContents.add(view);
	    dialogContents.setCellHorizontalAlignment(
	    		view, HasHorizontalAlignment.ALIGN_CENTER);
	    
	    // Add a close button at the bottom of the dialog
	    FormButton closeButton = new FormButton(I18nMessages.INSTANCE.sair(), null, 
	    		new FormWidget.HandlerValidation<String>() {
			    	public boolean handle(String valor) {
			    		FilterPesquisaHotel filter = new FilterPesquisaHotel();
			    		viewInfoPesquisaHotel.carregarFilterPesquisaHotel(filter);
			    		viewInfoQualidadePesquisaHotel.carregarFilterPesquisaHotel(filter);
			    		viewInfoQuantidadePesquisaHotel.carregarFilterPesquisaHotel(filter);
			    		dialogBox.hide();
			    		
			    		if (handlerAva != null) {
			    			handlerAva.handleSuccess(filter);
			    		}
			    		
			    		return true;
			    	}
	    		});
	    
	    dialogContents.add(closeButton);
	    dialogBox.setGlassEnabled(true);
	    dialogBox.setAnimationEnabled(true);
	   	// FIM - EXERC. 3
	    // Return the dialog box
	    return dialogBox;
	  }
	

}
